
using Youz.Common.Helper;
using Youz.Common.DAL;
using Youz.Common.Entity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Youz.Common.BLL
{
	/// <summary>
	/// Lang_LanguageType BLL Class
	/// </summary>
	public partial class Lang_LanguageTypeBLL : BaseBLL
	{
		Lang_LanguageTypeDAL dal;

		private const string CK_LanguageType_All = "LanguageType_All";
		
		public Lang_LanguageTypeBLL()
		{
			dal = new Lang_LanguageTypeDAL();
			base.ModuleName = "Lang_LanguageType";
			base.MenuName = "Lang_LanguageType";
		}

		#region  成员方法
		/// <summary>
		///  新增一条数据
		/// </summary>
		public bool Add(Lang_LanguageType model)
		{
			var r = dal.Add(model);

			if(r)
			{
				CacheHelper.RemoveCache(CK_LanguageType_All);
			}

			base.WriteOperationLog(OperationType.Add, r, Utility.GetJsonByObjectHaveTime(model));

			return r;
		}

		/// <summary>
		///  保存一条数据
		/// </summary>
		public bool Update(Lang_LanguageType model)
		{
			var r = dal.Update(model);

			if(r)
			{
				CacheHelper.RemoveCache(CK_LanguageType_All);
			}

			base.WriteOperationLog(OperationType.Update, r, Utility.GetJsonByObjectHaveTime(model));

			return r;
		}

		/// <summary>
		/// 删除一条数据
		///</summary>
		public bool Delete(String LanguageCode)
		{
			var r = dal.Delete(LanguageCode);

			if(r)
			{
				CacheHelper.RemoveCache(CK_LanguageType_All);
			}

			base.WriteOperationLog(OperationType.Delete, r, LanguageCode);

			return r;
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Lang_LanguageType Get(String LanguageCode)
		{
			return Gets().Where(m => m.LanguageCode.ToLower() == LanguageCode.ToLower()).SingleOrDefault();
		}

		/// <summary>
		/// 得到一个JSON对象
		/// </summary>
		public string GetJson(String LanguageCode)
		{
			return Utility.GetJsonByObjectHaveTime(Get(LanguageCode));
		}

		public List<Lang_LanguageType> Gets()
		{
			List<Lang_LanguageType> list = CacheHelper.GetCache(CK_LanguageType_All) as List<Lang_LanguageType>;

			if (list == null)
			{
				DataTable dt = dal.Gets(string.Empty);

				list = new EntityHelper<Lang_LanguageType>().FillEntity(dt);

				if (list != null)
				{
					CacheHelper.SetCache(CK_LanguageType_All, list);
				}
			}

			return list == null ? new List<Lang_LanguageType>() : list;
		}

		/// <summary>
		/// 獲取默認語言類型
		/// </summary>
		/// <returns></returns>
		public Lang_LanguageType GetDefault()
		{
			Lang_LanguageType type = Gets().Where(m => m.IsDefault == true).Take(1).SingleOrDefault();

			if (type == null)
			{
				type = Gets().SingleOrDefault();
			}

			return type;
		}

		/// <summary>
		/// 获得数据列表分页
		/// </summary>
		public DataTable Gets(string sqlWhere, params SqlParameter[] cmdParms)
		{
			return dal.Gets(sqlWhere, cmdParms);
		}
		#endregion  成员方法
	  
	}
}


  